package com.nova.bluetooth;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

import com.nova.bluetooth.base.BaseApp;
import com.nova.bluetooth.helpUtils.LogUtil;

public class MyApp extends BaseApp {
    // 私有静态实例
    private static volatile MyApp instance;

    // 提供公共静态方法获取实例
    public static MyApp getInstance() {
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        init();
        registerActivityLifecycleCallbacks(new AppLifecycleHandler());
    }



    public static class AppLifecycleHandler implements Application.ActivityLifecycleCallbacks {
        private static int resumed;

        @Override
        public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
        }

        @Override
        public void onActivityStarted(Activity activity) {
        }

        @Override
        public void onActivityResumed(Activity activity) {
            resumed++;
        }

        @Override
        public void onActivityPaused(Activity activity) {
            resumed--;
            if (resumed == 0) {
                // 应用退出前台
                onAppBackgrounded();
            }
        }

        @Override
        public void onActivityStopped(Activity activity) {
        }

        @Override
        public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
        }

        @Override
        public void onActivityDestroyed(Activity activity) {
        }

        public static boolean isApplicationVisible() {
            return resumed > 0;
        }

        private void onAppBackgrounded() {
            // 在这里处理应用退出前台的逻辑
            LogUtil.d("AppLifecycle:"+ "App is in the background");
        }
    }
}
